package com.unisci.db;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import com.unisci.android.utils.ProjectItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ProjectDB {
	public static final String DB_NAME = "db.db";
	private static final String TABLE_NAME = "";
	private SQLiteDatabase db;

	public ProjectDB(Context context) {
		db = context.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
	}

	public void saveRecord(String recordName, ProjectItem record) {
		float[] temp = new float[5];
		int[] time = new int[5];
		int[] temp_down = new int[5];
		int[] loop_stage = new int[5];
		int[] loop = new int[5];
		int index = record.getIndex();
		String date = record.getDate();
		String name = record.getName();
		String t = record.getT();

		// fill in temp
		for (int i = 0; i < 5; i++) {
			temp[i] = record.getTemp(i);
		}
		// fill in time
		for (int i = 0; i < 5; i++) {
			time[i] = record.getTime(i);
		}
		// fill in temp down
		for (int i = 0; i < 5; i++) {
			temp_down[i] = record.getTempDown(i);
		}
		// fill in loop stage
		for (int i = 0; i < 5; i++) {
			loop_stage[i] = record.getLoopStage(i);
		}
		// fill in loop
		for (int i = 0; i < 5; i++) {
			loop[i] = record.getLoop(i);
		}

		db.execSQL("CREATE table IF NOT EXISTS _"
				+ recordName
				+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT,iindex INTEGER,"
				+ "name TEXT, t TEXT,"
				+ "s1_temp FLOAT,s2_temp FLOAT,s3_temp FLOAT, s4_temp FLOAT, s5_temp FLOAT,"
				+ "s1_time INTEGER, s2_time INTEGER, s3_time INTEGER, s4_time INTEGER, s5_time INTEGER,"
				+ "s3_temp_down INTEGER," + "s4_loop_stage, s4_loop,"
				+ "date TEXT)");

		db.execSQL(
				"insert into _"
						+ recordName
						+ " (iindex, name, t, s1_temp, s2_temp, s3_temp, s4_temp, s5_temp,"
						+ "s1_time, s2_time, s3_time, s4_time, s5_time,"
						+ "s3_temp_down, s4_loop_stage, s4_loop, date) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
				new Object[] { index, name, t, temp[0], temp[1], temp[2],
						temp[3], temp[4], time[0], time[1], time[2], time[3],
						time[4], temp_down[2], loop_stage[3], loop[3], date });
	}

	public void update(String table, ProjectItem item, String name) {
		ContentValues cv = new ContentValues();
		cv.put("iindex", item.getIndex());
		cv.put("s1_temp", item.getTemp(0));
		cv.put("s2_temp", item.getTemp(1));
		cv.put("s3_temp", item.getTemp(2));
		cv.put("s4_temp", item.getTemp(3));
		cv.put("s5_temp", item.getTemp(4));
		cv.put("s1_time", item.getTime(0));
		cv.put("s2_time", item.getTime(1));
		cv.put("s3_time", item.getTime(2));
		cv.put("s4_time", item.getTime(3));
		cv.put("s5_time", item.getTime(4));
		cv.put("s3_temp_down", item.getTempDown(2));
		cv.put("s4_loop_stage", item.getLoopStage(3));
		cv.put("s4_loop", item.getLoop(3));
		cv.put("date", item.getDate());
		cv.put("name", item.getName());
		cv.put("t", item.getT());
		db.update("_" + table, cv, "name=?", new String[] { name });
	}

	public List<ProjectItem> getRecords(String recordName) {
		List<ProjectItem> list = new LinkedList<ProjectItem>();
		Cursor c = db.rawQuery(
				"SELECT * from _" + recordName + " ORDER BY _id", null);
		while (c.moveToNext()) {
			float[] temp = new float[5];
			int[] time = new int[5];
			int[] temp_down = new int[5];
			int[] loop_stage = new int[5];
			int[] loop = new int[5];
			int index;
			String date = "";
			String name = "";
			String t = "";

			index = c.getInt(c.getColumnIndex("iindex"));
			temp[0] = c.getFloat(c.getColumnIndex("s1_temp"));
			temp[1] = c.getFloat(c.getColumnIndex("s2_temp"));
			temp[2] = c.getFloat(c.getColumnIndex("s3_temp"));
			temp[3] = c.getFloat(c.getColumnIndex("s4_temp"));
			temp[4] = c.getFloat(c.getColumnIndex("s5_temp"));
			time[0] = c.getInt(c.getColumnIndex("s1_time"));
			time[1] = c.getInt(c.getColumnIndex("s2_time"));
			time[2] = c.getInt(c.getColumnIndex("s3_time"));
			time[3] = c.getInt(c.getColumnIndex("s4_time"));
			time[4] = c.getInt(c.getColumnIndex("s5_time"));
			temp_down[2] = c.getInt(c.getColumnIndex("s3_temp_down"));
			loop_stage[3] = c.getInt(c.getColumnIndex("s4_loop_stage"));
			loop[3] = c.getInt(c.getColumnIndex("s4_loop"));
			date = c.getString(c.getColumnIndex("date"));
			name = c.getString(c.getColumnIndex("name"));
			t = c.getString(c.getColumnIndex("t"));

			ProjectItem record = new ProjectItem();
			// fill in temp
			for (int i = 0; i < 5; i++) {
				record.setTemp(i, temp[i]);
			}
			// fill in time
			for (int i = 0; i < 5; i++) {
				record.setTime(i, time[i]);
			}
			// fill in temp down
			for (int i = 0; i < 5; i++) {
				record.setTempDown(i, temp_down[i]);
			}
			// fill in loop stage
			for (int i = 0; i < 5; i++) {
				record.setLoopStage(i, loop_stage[i]);
			}
			// fill in loop
			for (int i = 0; i < 5; i++) {
				record.setLoop(i, loop[i]);
			}
			record.setIndex(index);
			record.setDate(date);
			record.setName(name);
			record.setT(t);

			list.add(record);
		}
		c.close();
		Collections.reverse(list);

		return list;
	}

	public void delItem(String recordName, long date) {
		db.delete("_" + recordName, "date=?", new String[] { "" + date });
	}

	public List<String> getTables() {
		List<String> tables = new LinkedList<String>();
		Cursor cursor = db
				.rawQuery(
						"select name from sqlite_master where type='table' order by name",
						null);
		while (cursor.moveToNext()) {
			String name = cursor.getString(0);
			if (name.startsWith("_")) {
				Log.d("PCR", name);
				tables.add(name.substring(1));
			}
		}

		return tables;
	}

	public void delRecord(String recordName) {
		db.execSQL("DROP TABLE IF EXISTS _" + recordName);
	}

}
